# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=glsl-optimizer

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=r60842.9b2a2d2
pkgrel=1
pkgdesc="C++ library that takes GLSL shaders, does some GPU-independent optimizations on them and outputs GLSL back (mingw-w64)"
arch=('any')
url='https://github.com/aras-p/glsl-optimizer'
license=('MIT')
depends=("")
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc")
optdepends=()
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
replaces=()
backup=()
options=('!strip' 'staticlibs' '!emptydirs')
source=("${_realname}"::"git+https://github.com/aras-p/glsl-optimizer.git"
    0001-cmake-Link-OpenGL-with-glsl_test.patch
    0002-Fix-glslopt-compile-failure.patch
    0003-fix-tests-compile.patch
    0004-Fix-incorrect-Windows-64-hash_table_find-cast.patch
    )
md5sums=('SKIP'
         'a69f07671525c48ac0a9a0321fda2b42'
         '42190f0e6a8f434885e8347831f5d407'
         '2b647ac196a91e08bd7e5aa35c851cdd'
         '4223ad35f51d21a77b0b89c1cb8a26c0')

pkgver() {
  cd "$srcdir/${_realname}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir/${_realname}"
  patch -p1 -i ${srcdir}/0001-cmake-Link-OpenGL-with-glsl_test.patch
  patch -p1 -i ${srcdir}/0002-Fix-glslopt-compile-failure.patch
  patch -p1 -i ${srcdir}/0003-fix-tests-compile.patch
  patch -p1 -i ${srcdir}/0004-Fix-incorrect-Windows-64-hash_table_find-cast.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    ../${_realname}
    
    make
}

package() {
    cd ${srcdir}/build-${MINGW_CHOST}
    mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib}
  cp *.exe ${pkgdir}${MINGW_PREFIX}/bin/
  cp $srcdir/${_realname}/src/glsl/glsl_optimizer.h ${pkgdir}${MINGW_PREFIX}/include/
  cp libglsl_optimizer.a ${pkgdir}${MINGW_PREFIX}/lib/
}

